package UI;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.Executor;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;

import Commnads.Commands;
import Commnads.GuiCommands;

/**
 * This class creates the GUI.
 * We using here with the commands from {@link GuiCommands} class.
 * @author DROR
 */

public class OurGUI implements UI 
{ 
	private GuiCommands guiCommands; 
	private static Shell dialogShell;
	public static Display display;
	private File file;
	private ArrayList<String> discps;
	public static int flag;
	public static ProgressBar progressBar1;

	/**
	 * Constructor
	 * @param guiCom reference of {@link GuiCommands}
	 */
	public OurGUI(GuiCommands guiCom) 
	{
		this.guiCommands = guiCom; 
		file= null;
		flag=1;
		discps= new ArrayList<String>();
		ArrayList<String> di= guiCommands.getAllAlgos();
		int size= di.size();
		for(int i =0;i<size;i++)
			discps.add(guiCommands.getClassDisc(di.get(i)));
	}

	public OurGUI() {}

	/**
	 * In this function we initializing the display, and creating the GUI
	 */
	public void init() {
		try {
			display = Display.getDefault();
			open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * This function returns the Executor ( for shutting down)
	 * @return  {@link Executor} The executor from {@link Commands} class
	 */
	public Executor getExecutor()
	{
		return guiCommands.getExecutor(); 
	}

	public  void open() {
		try {
			final Shell shell = new Shell(display);
			dialogShell = new Shell(shell);
			dialogShell.setLayout(new FormLayout());	
			dialogShell.setSize(800,600);
			SWTResourceManager.registerResourceUser(dialogShell);
			CTabFolder tabfold = new CTabFolder(dialogShell, SWT.NONE); 

			/*********************************************************************/
			/*                       CONTROL MENU                                */
			/*********************************************************************/

			CTabItem control = new CTabItem(tabfold, SWT.NONE); 
			control.setText("Control and Inspection"); 
			control.setFont(SWTResourceManager.getFont("Times New Roman", 12, 3, false, false));

			Composite com = new Composite(tabfold, SWT.NONE);
			FormLayout composite1Layout = new FormLayout();
			FormData composite1LData = new FormData();
			com.setLayoutData(composite1LData);
			com.setLayout(composite1Layout);	 
			control.setControl(com); 

			/* progress bar properties */
			FormData progressBar1LData = new FormData();
			progressBar1LData.left =  new FormAttachment(0, 1000, 283);
			progressBar1LData.top =  new FormAttachment(0, 1000, 249);
			progressBar1LData.width = 485;
			progressBar1LData.height = 26;
			progressBar1 = new ProgressBar(com, SWT.NONE);
			progressBar1.setLayoutData(progressBar1LData);
			progressBar1.setMaximum(100);

			Label progress = new Label(com, SWT.NONE);
			FormData progressLData = new FormData();
			progressLData.left =  new FormAttachment(0, 1000, 283);
			progressLData.top =  new FormAttachment(0, 1000, 215);
			progressLData.width = 103;
			progressLData.height = 24;
			progress.setLayoutData(progressLData);
			progress.setText("Progress Bar");
			progress.setFont(SWTResourceManager.getFont("Tahoma", 11, 1, false, false));
			progress.setBackground(SWTResourceManager.getColor(128, 128, 255));

			/* button resume initialization properties */
			final Button resume = new Button(com, SWT.PUSH | SWT.CENTER);
			FormData resumeLData = new FormData();
			resumeLData.left =  new FormAttachment(0, 1000, 470);
			resumeLData.top =  new FormAttachment(0, 1000, 94);
			resumeLData.width = 83;
			resumeLData.height = 38;
			resume.setLayoutData(resumeLData);
			resume.setText("Resume");
			resume.setBackground(SWTResourceManager.getColor(0, 0, 0));
			resume.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			/* button pause initialization properties */
			final Button pause = new Button(com, SWT.PUSH | SWT.CENTER);
			FormData pauseLData = new FormData();
			pauseLData.left =  new FormAttachment(0, 1000, 356);
			pauseLData.top =  new FormAttachment(0, 1000, 94);
			pauseLData.width = 78;
			pauseLData.height = 37;
			pause.setLayoutData(pauseLData);
			pause.setText("Pause");
			pause.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			pause.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button stop initialization properties */
			final Button stopInit = new Button(com, SWT.PUSH | SWT.CENTER);
			FormData stopInitLData = new FormData();
			stopInitLData.left =  new FormAttachment(0, 1000, 384);
			stopInitLData.top =  new FormAttachment(0, 1000, 147);
			stopInitLData.width = 132;
			stopInitLData.height = 43;
			stopInit.setLayoutData(stopInitLData);
			stopInit.setText("Stop initialization");
			stopInit.setBackground(SWTResourceManager.getColor(0, 0, 0));
			stopInit.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			/* button start initialization properties */
			final Button staInit = new Button(com, SWT.PUSH | SWT.CENTER);
			FormData staInitLData = new FormData();
			staInitLData.left =  new FormAttachment(0, 1000, 384);
			staInitLData.top =  new FormAttachment(0, 1000, 34);
			staInitLData.width = 132;
			staInitLData.height = 44;
			staInit.setLayoutData(staInitLData);
			staInit.setText("Start initialization");
			staInit.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			staInit.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button Start listening properties */
			final Button start = new Button(com, SWT.PUSH | SWT.BORDER); 
			FormData startLData = new FormData(); 
			startLData.left =  new FormAttachment(0, 1000, 12);
			startLData.top =  new FormAttachment(0, 1000, 26);
			startLData.width = 134;
			startLData.height = 46;
			start.setLayoutData(startLData);
			start.setText("Start listening");
			start.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			start.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button Stop listening properties */
			final Button stop = new Button(com, SWT.PUSH | SWT.BORDER);
			FormData stopLData = new FormData();
			stopLData.left =  new FormAttachment(0, 1000, 12);
			stopLData.top =  new FormAttachment(0, 1000, 107);
			stopLData.width = 134;
			stopLData.height = 46;
			stop.setLayoutData(stopLData);
			stop.setText("Stop listening");
			stop.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			stop.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button Show log properties */
			final Button log = new Button(com, SWT.PUSH | SWT.BORDER);			
			FormData logLData = new FormData();
			logLData.left =  new FormAttachment(0, 1000, 12);
			logLData.top =  new FormAttachment(0, 1000, 187);
			logLData.width = 134;
			logLData.height = 46;
			log.setLayoutData(logLData);
			log.setText("Show System Log");
			log.setBackground(SWTResourceManager.getColor(0, 0, 0));
			log.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			/* button Clear system properties */
			final Button clear = new Button(com, SWT.PUSH | SWT.BORDER);
			FormData clearLData = new FormData();
			clearLData.left =  new FormAttachment(0, 1000, 12);
			clearLData.top =  new FormAttachment(0, 1000, 269);
			clearLData.width = 134;
			clearLData.height = 46;
			clear.setLayoutData(clearLData);
			clear.setText("Clean tags table");
			clear.setBackground(SWTResourceManager.getColor(0, 0, 0));
			clear.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			start.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == start)
					{
						guiCommands.startListening();
					}
				}}); /* of Start listening listener */

			stop.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == stop)
					{
						guiCommands.stopListening();
					}
				}}); /* of Stop listening listener */

			clear.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == clear)
					{
						guiCommands.cleanSystem();
						flag=1;
					}
				}
			});  /* of Clear system listener */

			staInit.addListener(SWT.Selection ,new Listener(){

				public void handleEvent(Event event) 
				{
					if(event.widget==staInit)
					{	
						guiCommands.startInitialization();
					} 
				}
			});	/* of start initialization listener */

			stopInit.addListener(SWT.Selection ,new Listener(){

				public void handleEvent(Event event) 
				{
					if(event.widget==stopInit)
					{
						guiCommands.stopInitialization();
					} 
				}
			});	/* of stop initialization listener */

			pause.addListener(SWT.Selection ,new Listener(){

				public void handleEvent(Event event) 
				{
					if(event.widget==pause)
					{
						guiCommands.pauseInitialization();
					} 
				}
			});	/* of pause initialization listener */

			resume.addListener(SWT.Selection ,new Listener(){

				public void handleEvent(Event event) 
				{
					if(event.widget==resume)
					{
						guiCommands.resumeInitialization();
					} 
				}
			});	/* of resume initialization listener */

			log.addListener(SWT.Selection ,new Listener(){

				public void handleEvent(Event event) 
				{
					if(event.widget==log)
					{
						final Shell showlog = new Shell();
						showlog.setSize(970,650);
						showlog.setText("System Log");
						Composite c3 = new Composite(showlog, SWT.NONE);
						SWTResourceManager.registerResourceUser(showlog);
						showlog.setLayout(new FormLayout());

						FormLayout c3Layout = new FormLayout();
						FormData c3Data = new FormData();
						c3Data.left =  new FormAttachment(0, 1000, 0);
						c3Data.top =  new FormAttachment(0, 1000, 0);
						c3Data.width = 970;
						c3Data.height = 656;
						c3.setLayoutData(c3Data);
						c3.setLayout(c3Layout);

						FormData table2LData1 = new FormData();
						table2LData1.left =  new FormAttachment(0, 1000, 12);
						table2LData1.top =  new FormAttachment(0, 1000,20);
						table2LData1.width = 890;
						table2LData1.height = 535;
						Table table2 = new Table(c3, SWT.MULTI | SWT.BORDER | SWT.SCROLL_LINE);
						table2.setLayoutData(table2LData1);
						table2.setLinesVisible(true);
						table2.setHeaderVisible(true);
						table2.setFont(SWTResourceManager.getFont("Bookman Old Style", 11, 0, false, false));

						TableColumn tableColumn3 = new TableColumn(table2, SWT.NONE);
						tableColumn3.setText("The operation by date");
						tableColumn3.setWidth(980);

						showlog.open();

						ArrayList<String> logs = guiCommands.getLog();
						if (logs != null)
						{
							table2.removeAll();
							Iterator<String> it =logs.iterator();
							while (it.hasNext())
							{
								TableItem c= new TableItem(table2, SWT.NONE);
								c.setText(it.next());
							}
						}
					}
				}
			});  /* of show log listener */

			/*********************************************************************/
			/*                       OPTIONS MENU                                */
			/*********************************************************************/

			CTabItem options = new CTabItem(tabfold, SWT.NONE); 
			options.setText("Options and Definitions"); 
			options.setFont(SWTResourceManager.getFont("Times New Roman", 12, 3, false, false));

			Composite c2 = new Composite(tabfold, SWT.NONE);
			FormLayout composite12Layout = new FormLayout();
			FormData composite12LData = new FormData();
			c2.setLayoutData(composite12LData);
			c2.setLayout(composite12Layout);
			options.setControl(c2);

			/* button Select directory properties */
			final Button selectDir = new Button(c2, SWT.PUSH | SWT.BORDER); 
			FormData selectDirLData = new FormData(); 
			selectDirLData.left =  new FormAttachment(0, 1000, 14);
			selectDirLData.top =  new FormAttachment(0, 1000, 46);
			selectDirLData.width = 177;
			selectDirLData.height = 46;
			selectDir.setLayoutData(selectDirLData);
			selectDir.setText("Select Directory ");
			selectDir.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			selectDir.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button Remove directory properties */
			final Button button3 = new Button(c2, SWT.PUSH | SWT.CENTER);
			FormData button3LData = new FormData();
			button3LData.left =  new FormAttachment(0, 1000, 14);
			button3LData.top =  new FormAttachment(0, 1000, 116);
			button3LData.width = 177;
			button3LData.height = 46;
			button3.setLayoutData(button3LData);
			button3.setText("Show / Remove Directories");
			button3.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			button3.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button Add expression properties */
			final Button addexp = new Button(c2, SWT.PUSH | SWT.BORDER);
			FormData select2DirLData = new FormData();
			select2DirLData.left =  new FormAttachment(0, 1000, 14);
			select2DirLData.top =  new FormAttachment(0, 1000, 186);
			select2DirLData.width = 177;
			select2DirLData.height = 46;
			addexp.setLayoutData(select2DirLData);
			addexp.setText("Add regular expression ");
			addexp.setBackground(SWTResourceManager.getColor(0, 0, 0));
			addexp.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			/* button Assign expression properties */
			final Button assign = new Button(c2, SWT.PUSH | SWT.BORDER);
			FormData assignDirLData = new FormData();
			assignDirLData.left =  new FormAttachment(0, 1000, 14);
			assignDirLData.top =  new FormAttachment(0, 1000, 256);
			assignDirLData.width = 177;
			assignDirLData.height = 46;
			assign.setLayoutData(assignDirLData);
			assign.setText("Assign expression to Dir");
			assign.setBackground(SWTResourceManager.getColor(0, 0, 0));
			assign.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			Label choose = new Label(c2, SWT.NONE);
			FormData chooseLData = new FormData();
			chooseLData.left =  new FormAttachment(0, 1000, 271);
			chooseLData.top =  new FormAttachment(0, 1000, 17);
			chooseLData.width = 400;
			chooseLData.height = 50;
			choose.setLayoutData(chooseLData);
			choose.setText("Please choose an algorithm for tagging:");
			choose.setFont(SWTResourceManager.getFont("Segoe Print", 14, 1, false, false));

			final Button done = new Button(c2, SWT.PUSH | SWT.BORDER);
			FormData doneLData = new FormData();
			doneLData.left =  new FormAttachment(0, 1000, 655);
			doneLData.top =  new FormAttachment(0, 1000, 180);
			doneLData.width = 80;
			doneLData.height = 36;
			done.setLayoutData(doneLData);
			done.setText("Apply");
			done.setBackground(SWTResourceManager.getColor(108, 221, 168));
			done.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			final Button addAlg = new Button(c2, SWT.PUSH | SWT.BORDER);
			FormData addAlgLData = new FormData();
			addAlgLData.left =  new FormAttachment(0, 1000, 620);
			addAlgLData.top =  new FormAttachment(0, 1000, 230);
			addAlgLData.width = 150;
			addAlgLData.height = 36;
			addAlg.setLayoutData(addAlgLData);
			addAlg.setText("Add new Algorithm");
			addAlg.setBackground(SWTResourceManager.getColor(108, 221, 168));
			addAlg.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			FormData tableLData = new FormData();
			tableLData.left =  new FormAttachment(0, 1000, 419);
			tableLData.top =  new FormAttachment(0, 1000, 71);
			tableLData.width = 155;
			tableLData.height = 225;
			final Table table = new Table(c2,  SWT.BORDER | SWT.FULL_SELECTION| SWT.CHECK);
			table.setLayoutData(tableLData);
			table.setLinesVisible (true);
			table.setHeaderVisible (true);
			table.setFont(SWTResourceManager.getFont("Tahoma", 15, 0, false, false));	
			table.setBackground(SWTResourceManager.getColor(108, 221, 168));

			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
			tableColumn.setText("The Algorithms:");
			tableColumn.setWidth(170);

			final TableItem alg1 = new TableItem (table, SWT.CHECK);;
			alg1.setText("Key words");
			alg1.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));

			final TableItem alg2 = new TableItem (table, SWT.CHECK);
			alg2.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));
			alg2.setText("Ascii file");

			final TableItem alg3 = new TableItem (table, SWT.CHECK);
			alg3.setText("Size of file");
			alg3.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));

			final TableItem alg4 = new TableItem (table, SWT.CHECK);
			alg4.setText("Creation Date");
			alg4.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));

			ArrayList<String> algs= guiCommands.getAllAlgos();
			int size= algs.size();
			if (size>4)
				for(int i =4;i<size;i++)
				{
					TableItem alg = new TableItem (table, SWT.CHECK);
					String name= guiCommands.getNameofAlgo(algs.get(i));
					alg.setText(name);
					alg.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));
				}

			final StyledText styledText1 = new StyledText(c2, SWT.NONE);
			FormLayout styledText1Layout = new FormLayout();
			styledText1.setLayout(styledText1Layout);
			FormData styledText1LData = new FormData();
			styledText1LData.left =  new FormAttachment(0, 1000, 116);
			styledText1LData.top =  new FormAttachment(0, 1000, 334);
			styledText1LData.width = 630;
			styledText1LData.height = 25;
			styledText1.setLayoutData(styledText1LData);
			styledText1.setText("Hey, to get an information about some algorithm, just please press on it");
			styledText1.setFont(SWTResourceManager.getFont("Rockwell", 12, 1, false, false));
			styledText1.setBackground(SWTResourceManager.getColor(255, 255, 128));

			addAlg.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) 
				{
					if(event.widget==addAlg)
					{
						final Shell addAlgo = new Shell();
						addAlgo.setSize(490,400);
						addAlgo.setText("Adding new algorithm");
						Composite c1 = new Composite(addAlgo, SWT.NONE);
						SWTResourceManager.registerResourceUser(addAlgo);
						addAlgo.setLayout(new FormLayout());

						FormLayout composite1Layout = new FormLayout();
						FormData composite1LData = new FormData();
						composite1LData.left =  new FormAttachment(0, 1000, 0);
						composite1LData.top =  new FormAttachment(0, 1000, 0);
						composite1LData.width = 445;
						composite1LData.height = 293;
						c1.setLayoutData(composite1LData);
						c1.setLayout(composite1Layout);

						final Button cancel = new Button(c1, SWT.PUSH | SWT.CENTER);
						FormData cancelLData = new FormData();
						cancelLData.left =  new FormAttachment(0, 1000, 181);
						cancelLData.top =  new FormAttachment(0, 1000, 216);
						cancelLData.width = 94;
						cancelLData.height = 32;
						cancel.setLayoutData(cancelLData);
						cancel.setText("Cancel");
						cancel.setBackground(SWTResourceManager.getColor(0, 0, 0));
						cancel.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						final Button ok = new Button(c1, SWT.PUSH | SWT.CENTER);
						FormData okLData = new FormData();
						okLData.left =  new FormAttachment(0, 1000, 67);
						okLData.top =  new FormAttachment(0, 1000, 216);
						okLData.width = 83;
						okLData.height = 32;
						ok.setLayoutData(okLData);
						ok.setText("Ok");
						ok.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
						ok.setBackground(SWTResourceManager.getColor(0, 0, 0));

						FormData text2LData = new FormData();
						text2LData.left =  new FormAttachment(0, 1000, 237);
						text2LData.top =  new FormAttachment(0, 1000, 156);
						text2LData.width = 197;
						text2LData.height = 22;
						final Text text2 = new Text(c1, SWT.NONE);
						text2.setLayoutData(text2LData);

						Label name = new Label(c1, SWT.NONE);
						FormData nameLData = new FormData();
						nameLData.left =  new FormAttachment(0, 1000, 37);
						nameLData.top =  new FormAttachment(0, 1000, 154);
						nameLData.width = 188;
						nameLData.height = 28;
						name.setLayoutData(nameLData);
						name.setText("Insert algorithm's name: ");
						name.setFont(SWTResourceManager.getFont("Tahoma", 11, 1, false, false));

						final Button select = new Button(c1, SWT.PUSH | SWT.CENTER);
						FormData selectLData = new FormData();
						selectLData.left =  new FormAttachment(0, 1000, 44);
						selectLData.top =  new FormAttachment(0, 1000, 31);
						selectLData.width = 98;
						selectLData.height = 34;
						select.setLayoutData(selectLData);
						select.setText("Select class");
						select.setBackground(SWTResourceManager.getColor(0, 0, 0));
						select.setFont(SWTResourceManager.getFont("MS Reference Sans Serif", 9, 1, false, false));

						Label label1 = new Label(c1, SWT.NONE);
						label1.setText("class's path:");
						FormData label1LData = new FormData();
						label1LData.left =  new FormAttachment(0, 1000, 37);
						label1LData.top =  new FormAttachment(0, 1000, 100);
						label1LData.width = 105;
						label1LData.height = 28;
						label1.setLayoutData(label1LData);
						label1.setFont(SWTResourceManager.getFont("Tahoma", 11, 1, false, false));

						final Text text1 = new Text(c1, SWT.NONE);
						FormData text1LData = new FormData();
						text1LData.left =  new FormAttachment(0, 1000, 142);
						text1LData.top =  new FormAttachment(0, 1000, 100);
						text1LData.width = 290;
						text1LData.height = 22;
						text1.setLayoutData(text1LData);

						addAlgo.open();

						select.addListener(SWT.Selection ,new Listener() {
							public void handleEvent(Event event) 
							{
								if(event.widget == select)
								{
									JFrame jframe = new JFrame();
									String filename = File.separator+"tmp";
									JFileChooser jfc = new JFileChooser(new File(filename));
									/* restricting the choose to only files */
									jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
									jfc.showOpenDialog(jframe);
									File file1 =jfc.getSelectedFile();
									if (file1==null) 	/* checking selection */
										jframe.dispose();
									else
									{
										String algClassPath = file1.toString(); /* get the path by string */
										String words[]=algClassPath.split("\\\\");
										int len= words.length;
										String last[]=words[len-1].split("\\.");
										if (last[last.length-1].equals("class"))			
											text1.setText(algClassPath); /* set the label accordingly */
										else
											new ErrorMassage("Error: Invalid file");
										jframe.dispose();
										addAlgo.forceActive();	
									}
								}
							}}); /* of Select file listener */

						/* adding a listener for OK button */
						ok.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==ok)
								{
									/* getting the expression from the label */
									String path=text1.getText();
									if (path.length()>0)
									{
										String algName=text2.getText();
										if (algName.length()>0) /* checking that we get the expression*/
										{  
											guiCommands.insertToAllAlgos(algName,path);
											TableItem newalg = new TableItem (table, SWT.CHECK);

											discps.add(guiCommands.getClassDisc(path));
											newalg.setText(algName);
											newalg.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));
											addAlgo.close();
										}
										else	/* sending Error by massageBox */
											new ErrorMassage("Error: Please insert algorithm's name");
									}
									else
										new ErrorMassage("Error: Please select file first");
								}
							}
						});	/* of OK listener */

						/*	adding a listener for Cancel button */
						cancel.addListener(SWT.Selection ,new Listener(){
							public void handleEvent(Event event) 
							{
								if(event.widget==cancel)
									addAlgo.close();
							}
						}); /* of Cancel listener */
					}
				}
			});

			done.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) 
				{
					if(event.widget==done)
					{
						ArrayList<String> arr = new ArrayList<String>();
						for(int i=0;i<table.getItemCount();i++)
						{
							if(table.getItem(i).getChecked())
							{
								String alg= table.getItem(i).getText();
								arr.add(alg);
							}
						}
						guiCommands.dynamicLoad(arr);
					}
				}
			});

			table.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) 
				{
					TableItem item= (TableItem) event.item;
					for(int i=0;i<table.getItemCount();i++)
						if(table.getItem(i).getText().equals(item.getText()))       
							styledText1.setText(discps.get(i));
				}
			});

			table.addMouseMoveListener(new MouseMoveListener() {
				public void mouseMove(MouseEvent arg0) 
				{
					table.setToolTipText("Please press on Apply, when you finish to select");
				}});


			selectDir.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == selectDir)
					{
						JFrame jframe = new JFrame();
						String filename = File.separator+"tmp";
						JFileChooser jfc = new JFileChooser(new File(filename));
						/* restricting the choose to only directories */
						jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
						jfc.showOpenDialog(jframe);
						File file =jfc.getSelectedFile();
						if (file==null) /* checking selection */
							jframe.dispose();
						else
						{
							Path path = file.toPath(); 			
							guiCommands.selectDir(path); /* adding the path to the hashMap and DB */
							jframe.dispose();
							dialogShell.forceActive();
						}
					}
				}}); /* of select directory listener */

			addexp.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == addexp)
					{
						/* Shell properties */

						final Shell addexpress = new Shell(display);
						addexpress.setSize(340,390);
						addexpress.setText("Adding regular expression");
						Composite composite2 = new Composite(addexpress, SWT.NONE);
						SWTResourceManager.registerResourceUser(addexpress);
						addexpress.setLayout(new FormLayout());

						FormLayout composite2Layout = new FormLayout();
						FormData composite2LData = new FormData();
						composite2LData.left =  new FormAttachment(0, 1000, 0);
						composite2LData.top =  new FormAttachment(0, 1000, 0);
						composite2LData.width = 358;
						composite2LData.height = 315;
						composite2.setLayoutData(composite2LData);
						composite2.setLayout(composite2Layout);

						/* List of expressions properties */
						FormData list1LData = new FormData();
						list1LData.left =  new FormAttachment(0, 1000, 22);
						list1LData.top =  new FormAttachment(0, 1000, 106);
						list1LData.width = 109;
						list1LData.height = 164;
						final List list1 = new List(composite2, SWT.NONE);
						list1.setLayoutData(list1LData);
						final ArrayList<String> allExprs = new ArrayList<String>();  

						if (guiCommands.getAllExpressions()!=null)
							allExprs.addAll(guiCommands.getAllExpressions());
						int len= allExprs.size();
						for (int i=0;i<len;i++)
							list1.add(allExprs.get(i));

						Label allexps = new Label(composite2, SWT.NONE);
						FormData allexpsLData = new FormData();
						allexpsLData.left =  new FormAttachment(0, 1000, 22);
						allexpsLData.top =  new FormAttachment(0, 1000, 80);
						allexpsLData.width = 112;
						allexpsLData.height = 20;
						allexps.setLayoutData(allexpsLData);
						allexps.setText("The Expressions: ");
						allexps.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						final Text text1 = new Text(composite2, SWT.NONE);
						FormData text1LData = new FormData();
						text1LData.left =  new FormAttachment(0, 1000, 22);
						text1LData.top =  new FormAttachment(0, 1000, 32);
						text1LData.width = 233;
						text1LData.height = 24;
						text1.setLayoutData(text1LData);

						Label insert = new Label(composite2, SWT.NONE);
						FormData insertLData = new FormData();
						insertLData.left =  new FormAttachment(0, 1000, 22);
						insertLData.top =  new FormAttachment(0, 1000, 12);
						insertLData.width = 162;
						insertLData.height = 21;
						insert.setLayoutData(insertLData);
						insert.setText("Enter the expression:");
						insert.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						/* button Insert properties */
						final Button ins = new Button(composite2 , SWT.PUSH);
						FormData insLData = new FormData();
						insLData.left =  new FormAttachment(0, 1000, 171);
						insLData.top =  new FormAttachment(0, 1000, 108);
						insLData.width = 69;
						insLData.height = 32;
						ins.setLayoutData(insLData);
						ins.setText("Insert");
						ins.setBackground(SWTResourceManager.getColor(0, 0, 0));
						ins.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						/* button ok properties */
						final Button ok = new Button(composite2 , SWT.PUSH);
						FormData button1LData = new FormData();
						button1LData.left =  new FormAttachment(0, 1000, 171);
						button1LData.top =  new FormAttachment(0, 1000, 188);
						button1LData.width = 69;
						button1LData.height = 32;
						ok.setLayoutData(button1LData);
						ok.setText("Ok");
						ok.setBackground(SWTResourceManager.getColor(0, 0, 0));
						ok.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						/* button Cancel properties */
						final Button cancel = new Button(composite2, SWT.PUSH);
						FormData cancelLData = new FormData();
						cancelLData.left =  new FormAttachment(0, 1000, 171);
						cancelLData.top =  new FormAttachment(0, 1000, 230);
						cancelLData.width = 69;
						cancelLData.height = 33;
						cancel.setLayoutData(cancelLData);
						cancel.setText("cancel");
						cancel.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
						cancel.setBackground(SWTResourceManager.getColor(0, 0, 0));

						final Button remove = new Button(composite2, SWT.PUSH | SWT.CENTER);
						FormData removeLData = new FormData();
						removeLData.left =  new FormAttachment(0, 1000, 171);
						removeLData.top =  new FormAttachment(0, 1000, 148);
						removeLData.width = 69;
						removeLData.height = 33;
						remove.setLayoutData(removeLData);
						remove.setText("Remove");
						remove.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
						remove.setBackground(SWTResourceManager.getColor(0, 0, 0));
						final ArrayList<String> addexps= new ArrayList<String>();
						final ArrayList<String> remexps= new ArrayList<String>();
						addexpress.open();

						/* adding a listener for Insert button */
						ins.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==ins)
								{
									/* getting the expression from the label */
									String expression=text1.getText();
									if ((expression.length()>0)) /* checking that we get the expression*/
									{  
										addexps.add(expression);
										list1.add(expression);
										text1.setText("");
									}
									else	/* sending Error by massageBox */
										new ErrorMassage("Error: Please enter expression");
								}
							}
						});	/* of Insert listener */

						/* adding a listener for OK button */
						ok.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==ok)
								{

									Iterator<String> it =addexps.iterator();
									while (it.hasNext())
										guiCommands.addExpression(it.next());

									Iterator<String> iter =remexps.iterator();
									while (iter.hasNext())
										guiCommands.removeExpression(iter.next());

									addexpress.close();
								}
							}
						});	/* of OK listener */

						/*	adding a listener for Cancel button */
						cancel.addListener(SWT.Selection ,new Listener(){
							public void handleEvent(Event event) 
							{
								if(event.widget==cancel)
									addexpress.close();
							}
						}); /* of Cancel listener */

						remove.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==remove)
								{
									int index=list1.getSelectionIndex(); /* getting the selection */
									if ((index>-1)) 
									{
										String expName= list1.getItem(index);	
										remexps.add(expName);
										list1.remove(expName);
									} 
								}
							}
						});	/* of remove all listener */

					}}}); /* of add expression listener */

			button3.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == button3)
					{
						/* Shell properties */
						final Shell remDir = new Shell();
						remDir.setSize(560, 400);
						remDir.setText("Remove Dir");
						Composite composite1 = new Composite(remDir, SWT.NONE);
						SWTResourceManager.registerResourceUser(remDir);
						remDir.setLayout(new FormLayout());

						FormLayout composite1Layout = new FormLayout();
						FormData composite1LData = new FormData();
						composite1LData.left =  new FormAttachment(0, 1000, 5);
						composite1LData.top =  new FormAttachment(0, 1000, 6);
						composite1LData.width = 550;
						composite1LData.height = 380;
						composite1.setLayoutData(composite1LData);
						composite1.setLayout(composite1Layout);

						final Button remove = new Button(composite1, SWT.PUSH | SWT.CENTER);
						FormData removeLData = new FormData();
						removeLData.left =  new FormAttachment(0, 1000, 18);
						removeLData.top =  new FormAttachment(0, 1000,91);
						removeLData.width = 76;
						removeLData.height = 34;
						remove.setLayoutData(removeLData);
						remove.setText("Remove");
						remove.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
						remove.setBackground(SWTResourceManager.getColor(0, 0, 0));

						final Button cancel = new Button(composite1, SWT.PUSH | SWT.CENTER);
						FormData cancelLData = new FormData();
						cancelLData.left =  new FormAttachment(0, 1000, 18);
						cancelLData.top =  new FormAttachment(0, 1000, 189);
						cancelLData.width = 76;
						cancelLData.height = 34;
						cancel.setLayoutData(cancelLData);
						cancel.setText("cancel");
						cancel.setBackground(SWTResourceManager.getColor(0, 0, 0));
						cancel.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						final Button ok = new Button(composite1, SWT.PUSH | SWT.CENTER);
						FormData OkLData = new FormData();
						OkLData.left =  new FormAttachment(0, 1000, 18);
						OkLData.top =  new FormAttachment(0, 1000, 150);
						OkLData.width = 76;
						OkLData.height = 30;
						ok.setLayoutData(OkLData);
						ok.setText("Ok");
						ok.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
						ok.setBackground(SWTResourceManager.getColor(0, 0, 0));

						Label label2 = new Label(composite1, SWT.NONE);
						FormData label2LData = new FormData();
						label2LData.left =  new FormAttachment(0, 1000, 150);
						label2LData.top =  new FormAttachment(0, 1000, 49);
						label2LData.width = 230;
						label2LData.height = 17;
						label2.setLayoutData(label2LData);
						label2.setText("All the listened directories:");
						label2.setFont(SWTResourceManager.getFont("Bookman Old Style", 11, 1, false, false));

						FormData list1LData = new FormData();
						list1LData.left =  new FormAttachment(0, 1000, 150);
						list1LData.top =  new FormAttachment(0, 1000, 79);
						list1LData.width = 330;
						list1LData.height = 240;
						final List dirs = new List(composite1, SWT.BORDER | SWT.SCROLL_LINE | SWT.V_SCROLL);
						dirs.setLayoutData(list1LData);
						final ArrayList<String> allDirs = new ArrayList<String>();
						if 	(guiCommands.getDirectories()!=null)
							allDirs.addAll(guiCommands.getDirectories());
						int len= allDirs.size();
						for (int i=0;i<len;i++)
							dirs.add(allDirs.get(i));

						Label label1 = new Label(composite1, SWT.NONE);
						FormData label1LData = new FormData();
						label1LData.left =  new FormAttachment(0, 1000, 18);
						label1LData.top =  new FormAttachment(0, 1000, 16);
						label1LData.width = 257;
						label1LData.height = 27;
						label1.setLayoutData(label1LData);
						label1.setText("Select a directory to remove:   ");
						label1.setFont(SWTResourceManager.getFont("Bookman Old Style", 13, 1, false, false));
						final ArrayList<String> remdirs= new ArrayList<String>();
						remDir.open();

						remove.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==remove)
								{
									int index=dirs.getSelectionIndex(); /* getting the selection */
									if ((index>-1)) /* checking that we actually select */
									{
										String dirName= dirs.getItem(index);	/* getting the directory */  
										remdirs.add(dirName);
										guiCommands.removeDir(dirName);
										dirs.remove(dirName);
									}
									else
										/* sending Error by massageBox */
										new ErrorMassage("Error: Please select directory first.");
								} 
							}
						});	/* of OK listener */

						/* adding a listener for OK button */
						ok.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==ok)
								{
									remDir.close();
								} 
							}
						});	/* of OK listener */

						/*	adding a listener for Cancel button */
						cancel.addListener(SWT.Selection ,new Listener(){
							public void handleEvent(Event event) 
							{
								if(event.widget==cancel)
									remDir.close();
							}
						}); /* of Cancel listener */


					}}}); /* of remove directory listener */

			assign.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == assign)
					{
						/* Shell properties */
						final Shell assign = new Shell();
						assign.setSize(750,410);
						assign.setText("Assign expression to Dir");
						Composite c4 = new Composite(assign, SWT.NONE);
						SWTResourceManager.registerResourceUser(assign);
						assign.setLayout(new FormLayout());

						FormLayout c4Layout = new FormLayout();
						FormData c4LData = new FormData();
						c4LData.left =  new FormAttachment(0, 1000, 8);
						c4LData.top =  new FormAttachment(0, 1000, 5);
						c4LData.width = 740;
						c4LData.height = 360;
						c4.setLayoutData(c4LData);
						c4.setLayout(c4Layout);

						final Button ass = new Button(c4, SWT.PUSH | SWT.CENTER);
						FormData assLData = new FormData();
						assLData.left =  new FormAttachment(0, 1000, 376);
						assLData.top =  new FormAttachment(0, 1000, 105);
						assLData.width = 71;
						assLData.height = 36;
						ass.setLayoutData(assLData);
						ass.setText("Assign");
						ass.setFont(SWTResourceManager.getFont("Lucida Calligraphy", 10, 1, false, false));
						ass.setBackground(SWTResourceManager.getColor(0, 0, 0));

						FormData list1LData = new FormData();
						list1LData.left =  new FormAttachment(0, 1000, 465);
						list1LData.top =  new FormAttachment(0, 1000, 99);
						list1LData.width = 160;
						list1LData.height = 122;
						final List list1 = new List(c4, SWT.MULTI | SWT.BORDER | SWT.SCROLL_LINE);
						list1.setLayoutData(list1LData);

						FormData text1LData = new FormData();
						text1LData.left =  new FormAttachment(0, 1000, 465);
						text1LData.top =  new FormAttachment(0, 1000, 47);
						text1LData.width = 230;
						text1LData.height = 20;
						final Text text1 = new Text(c4, SWT.NONE);
						text1.setLayoutData(text1LData);

						Label label3 = new Label(c4, SWT.NONE);
						FormData label3LData = new FormData();
						label3LData.left =  new FormAttachment(0, 1000, 375);
						label3LData.top =  new FormAttachment(0, 1000, 47);
						label3LData.width = 125;
						label3LData.height = 25;
						label3.setLayoutData(label3LData);
						label3.setText("Selected dir:");
						label3.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));

						Label label2 = new Label(c4, SWT.NONE);
						FormData label2LData = new FormData();
						label2LData.left =  new FormAttachment(0, 1000, 255);
						label2LData.top =  new FormAttachment(0, 1000, 18);
						label2LData.width = 89;
						label2LData.height = 14;
						label2.setLayoutData(label2LData);
						label2.setText("Expressions:");
						label2.setFont(SWTResourceManager.getFont("Narkisim", 12, 1, false, false));

						Label label1 = new Label(c4, SWT.NONE);
						FormData label1LData = new FormData();
						label1LData.left =  new FormAttachment(0, 1000, 18);
						label1LData.top =  new FormAttachment(0, 1000, 18);
						label1LData.width = 88;
						label1LData.height = 14;
						label1.setLayoutData(label1LData);
						label1.setText("Direrctories:");
						label1.setFont(SWTResourceManager.getFont("Narkisim", 12, 1, false, false));

						Button cancel = new Button(c4, SWT.PUSH | SWT.CENTER);
						FormData cancelLData = new FormData();
						cancelLData.left =  new FormAttachment(0, 1000, 547);
						cancelLData.top =  new FormAttachment(0, 1000, 253);
						cancelLData.width = 67;
						cancelLData.height = 29;
						cancel.setLayoutData(cancelLData);
						cancel.setText("Cancel");
						cancel.setBackground(SWTResourceManager.getColor(0, 0, 0));
						cancel.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

						final Button ok = new Button(c4, SWT.PUSH | SWT.CENTER);
						FormData okLData = new FormData();
						okLData.left =  new FormAttachment(0, 1000, 465);
						okLData.top =  new FormAttachment(0, 1000, 253);
						okLData.width = 63;
						okLData.height = 29;
						ok.setLayoutData(okLData);
						ok.setText("Ok");
						ok.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
						ok.setBackground(SWTResourceManager.getColor(0, 0, 0));

						FormData expressLData = new FormData();
						expressLData.left =  new FormAttachment(0, 1000,255);
						expressLData.top =  new FormAttachment(0, 1000, 38);
						expressLData.width = 100;
						expressLData.height = 210;
						final List express = new List(c4,SWT.MULTI | SWT.BORDER | SWT.SCROLL_LINE);
						express.setLayoutData(expressLData);
						final ArrayList<String> allExprs =new ArrayList<String>();  
						if (guiCommands.getAllExpressions()!=null)	
							allExprs.addAll(guiCommands.getAllExpressions());
						int len= allExprs.size();
						for (int i=0;i<len;i++)
							express.add(allExprs.get(i));

						FormData dirsLData = new FormData();
						dirsLData.left =  new FormAttachment(0, 1000,12);
						dirsLData.top =  new FormAttachment(0, 1000, 38);
						dirsLData.width =280;
						dirsLData.height = 210;
						final List dirs = new List(c4, SWT.MULTI | SWT.BORDER | SWT.SCROLL_LINE);
						dirs.setLayoutData(dirsLData);
						final ArrayList<String> allDirs = new ArrayList<String>();
						if (guiCommands.getDirectories()!=null)	
							allDirs.addAll(guiCommands.getDirectories());
						int len1= allDirs.size();
						for (int i=0;i<len1;i++)
							dirs.add(allDirs.get(i));

						Label label4 = new Label(c4, SWT.NONE);
						label4.setText("Selected expression/s:");
						FormData label4LData = new FormData();
						label4LData.left =  new FormAttachment(0, 1000, 375);
						label4LData.top =  new FormAttachment(0, 1000, 72);
						label4LData.width = 149;
						label4LData.height = 25;
						label4.setLayoutData(label4LData);
						label4.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));

						final Button refresh = new Button(c4, SWT.PUSH | SWT.CENTER);
						refresh.setText("Clear");
						FormData button1LData = new FormData();
						button1LData.left =  new FormAttachment(0, 1000, 376);
						button1LData.top =  new FormAttachment(0, 1000, 152);
						button1LData.width = 71;
						button1LData.height = 36;
						refresh.setLayoutData(button1LData);
						refresh.setBackground(SWTResourceManager.getColor(0, 0, 0));
						refresh.setFont(SWTResourceManager.getFont("Lucida Calligraphy", 10, 1, false, false));

						assign.open();

						/* adding a listener for assign button */
						ass.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==ass)
								{
									int index=dirs.getSelectionIndex(); /* getting the selection */ 
									if ((index>-1)) /* checking that we actually select */
										text1.setText(dirs.getItem(index));
									String[] arr= express.getSelection();
									for (int i=0;i<arr.length;i++)
										list1.add(arr[i]);
								}
							}
						});	/* of assign listener */

						refresh.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==refresh)
								{
									text1.setText("");
									list1.removeAll();
								}
							}
						});	/* of assign listener */

						/* adding a listener for OK button */
						ok.addListener(SWT.Selection ,new Listener(){

							public void handleEvent(Event event) 
							{
								if(event.widget==ok)
								{
									String dir= text1.getText();
									String[] arr= express.getSelection();
									ArrayList<String> selectedExps =  new ArrayList<String>();
									int len= arr.length;
									if (dir.length()>0 && len>0)
									{
										for (int i=0;i<len;i++)
											selectedExps.add(arr[i]);
										guiCommands.assignExpToDir(selectedExps, dir);	
										assign.close();
									}
									else
										new ErrorMassage("Error: Please select directory and expression first.");

								} 
							}
						});	/* of OK listener */

						/*	adding a listener for Cancel button */
						cancel.addListener(SWT.Selection ,new Listener(){
							public void handleEvent(Event event) 
							{
								assign.close();
							}
						}); /* of Cancel listener */

					}}}); /* of Assign listener */

			/*********************************************************************/
			/*                       SEARCH MENU                                 */
			/*********************************************************************/

			final CTabItem search = new CTabItem(tabfold, SWT.NONE); 
			search.setText("Searching");
			search.setFont(SWTResourceManager.getFont("Times New Roman", 12, 3, false, false));
			Composite composite1 = new Composite(tabfold, SWT.NONE);

			FormLayout composite1Layout1 = new FormLayout();
			FormData composite1LData1 = new FormData();
			composite1LData1.left =  new FormAttachment(0, 1000, 0);
			composite1LData1.top =  new FormAttachment(0, 1000, 5);
			composite1LData1.width = 754;
			composite1LData1.height = 418;
			composite1.setLayoutData(composite1LData1);
			composite1.setLayout(composite1Layout1);
			search.setControl(composite1);

			/* groop1 properties */
			Group group1 = new Group(composite1, SWT.NONE);
			FormLayout group1Layout = new FormLayout();
			group1.setLayout(group1Layout);
			FormData group1LData = new FormData();
			group1LData.left =  new FormAttachment(0, 1000, 20);
			group1LData.top =  new FormAttachment(0, 1000, 6);
			group1LData.width = 286;
			group1LData.height = 495;
			group1.setLayoutData(group1LData);
			group1.setText("Show Tag/s of file");
			group1.setFont(SWTResourceManager.getFont("Guttman-Aharoni", 11, 1, false, false));

			/* button Add new tag  properties */
			final Button add = new Button(group1, SWT.PUSH | SWT.CENTER);
			FormData button6LData = new FormData();
			button6LData.left =  new FormAttachment(0, 1000, 175);
			button6LData.top =  new FormAttachment(0, 1000, 160);
			button6LData.width = 68;
			button6LData.height = 32;
			add.setLayoutData(button6LData);
			add.setText("Add");
			add.setFont(SWTResourceManager.getFont("Guttman Adii", 9, 1, false, false));
			add.setBackground(SWTResourceManager.getColor(0, 0, 0));

			final Text text1 = new Text(group1, SWT.NONE);
			FormData text1LData = new FormData();
			text1LData.left =  new FormAttachment(0, 1000, 197);
			text1LData.top =  new FormAttachment(0, 1000, 124);
			text1LData.width = 74;
			text1LData.height = 19;
			text1.setLayoutData(text1LData);

			Label label9 = new Label(group1, SWT.NONE);
			FormData label9LData = new FormData();
			label9LData.left =  new FormAttachment(0, 1000, 146);
			label9LData.top =  new FormAttachment(0, 1000, 84);
			label9LData.width = 140;
			label9LData.height = 18;
			label9.setLayoutData(label9LData);
			label9.setText("Manual tagging");
			label9.setFont(SWTResourceManager.getFont("Bookman Old Style", 11, 1, false, false));

			Label label1 = new Label(group1, SWT.NONE);
			FormData label1LData = new FormData();
			label1LData.left =  new FormAttachment(0, 1000, 136);
			label1LData.top =  new FormAttachment(0, 1000, 120);
			label1LData.width = 55;
			label1LData.height = 18;
			label1.setLayoutData(label1LData);
			label1.setText("Add tag:");
			label1.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			/* button Remove tag  properties */
			final Button remove = new Button(group1, SWT.PUSH | SWT.CENTER);
			FormData button5LData = new FormData();
			button5LData.left =  new FormAttachment(0, 1000, 175);
			button5LData.top =  new FormAttachment(0, 1000, 205);
			button5LData.width = 68;
			button5LData.height = 32;
			remove.setLayoutData(button5LData);
			remove.setText("Remove");
			remove.setFont(SWTResourceManager.getFont("Guttman Adii", 9, 1, false, false));
			remove.setBackground(SWTResourceManager.getColor(0, 0, 0));

			/* button Select file properties */
			final Button sefile = new Button(group1, SWT.TOGGLE | SWT.CENTER);
			FormData button1LData = new FormData();
			button1LData.left =  new FormAttachment(0, 1000, 9);
			button1LData.top =  new FormAttachment(0, 1000, 15);
			button1LData.width = 113;
			button1LData.height = 34;
			sefile.setLayoutData(button1LData);
			sefile.setText("Select file");
			sefile.setFont(SWTResourceManager.getFont("Guttman Adii", 9, 1, false, false));
			sefile.setBackground(SWTResourceManager.getColor(0, 0, 0));

			final Label label2 = new Label(group1, SWT.NONE);
			FormData label2LData = new FormData();
			label2LData.left =  new FormAttachment(0, 1000, 14);
			label2LData.top =  new FormAttachment(0, 1000, 63);
			label2LData.width = 263;
			label2LData.height = 19;
			label2.setLayoutData(label2LData);
			label2.setText("File's tag/s");
			label2.setFont(SWTResourceManager.getFont("Guttman Adii", 9, 1, false, false));

			/* List tags properties */
			FormData list1LData = new FormData();
			list1LData.left =  new FormAttachment(0, 1000, 14);
			list1LData.top =  new FormAttachment(0, 1000, 90);
			list1LData.width = 107;
			list1LData.height = 229;
			final List list1 = new List(group1, SWT.BORDER );
			list1.setLayoutData(list1LData);

			sefile.addListener(SWT.Selection ,new Listener() {
				public void handleEvent(Event event) 
				{
					if(event.widget == sefile)
					{
						list1.removeAll();
						JFrame jframe = new JFrame();
						String filename = File.separator+"tmp";
						JFileChooser jfc = new JFileChooser(new File(filename));
						/* restricting the choose to only files */
						jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
						jfc.showOpenDialog(jframe);
						File file1 =jfc.getSelectedFile();
						if (file1==null) 	/* checking selection */
							jframe.dispose();
						else
						{
							file=file1;
							String filename1 = file1.getName(); /* get name of selected file */
							String path = file1.toString(); /* get the path by string */
							label2.setText(filename1+" tag/s"); /* set the label accordingly */
							ArrayList<String> arr= new ArrayList<String>(); 
							if	(guiCommands.getTag(path) !=null )
								arr.addAll(guiCommands.getTag(path));
							int len= arr.size();
							for (int i=0;i<len;i++)
								list1.add(arr.get(i));
							jframe.dispose();
							dialogShell.forceActive();
						}
					}
				}}); /* of Select file listener */

			add.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget==add)
					{
						String newTag=text1.getText(); /* get the tag from the label */
						if ( file == null) /* checking selection of file first */
							new ErrorMassage("Error: Please select a file first");
						else if ((newTag.length()>0)) /* checking that new tag inserted */
						{  
							String filename1 = file.getName(); /* get name of selected file */
							String path = file.toString(); /* get the path by string */
							guiCommands.addTag(filename1,newTag,path);
							list1.add(newTag);
							text1.setText("");
							flag=1;
						}
						else	/* sending Error by massageBox */
							new ErrorMassage("Error: Please enter new tag first");
					} 
				}
			});	// of add new tag listener

			remove.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget==remove)
					{
						int index=list1.getSelectionIndex(); /* getting the selection */ 
						if ((index>-1)) /* checking that we actually select */
						{
							String filename1 = file.getName(); /* get name of selected file */
							String path = file.toString(); /* get the path by string */
							String tag= list1.getItem(index);	/* get the tag */
							guiCommands.removeTag(filename1, tag, path);
							list1.remove(index);
							flag=1;
						}
						else	/* sending Error by massageBox */
							new ErrorMassage("Error: Please select tag first");
					} 
				}
			});	/* of remove tag listener */
			/******************************************************************************/

			/* group2 properties */
			Group	group2 = new Group(composite1, SWT.NONE);
			FormLayout group2Layout = new FormLayout();
			group2.setLayout(group2Layout);
			group2.setText("Search a file");
			FormData group2LData = new FormData();
			group2LData.left =  new FormAttachment(0, 1000, 329);
			group2LData.top =  new FormAttachment(0, 1000, 6);
			group2LData.width = 420;
			group2LData.height = 495;
			group2.setLayoutData(group2LData);
			group2.setFont(SWTResourceManager.getFont("Guttman Aharoni", 11, 1, false, false));

			/* button Refresh lists properties */
			final Button refresh = new Button(group2, SWT.PUSH | SWT.CENTER);
			FormData refreshLData = new FormData();
			refreshLData.left =  new FormAttachment(0, 1000, 74);
			refreshLData.top =  new FormAttachment(0, 1000, 150);
			refreshLData.width = 96;
			refreshLData.height = 31;
			refresh.setLayoutData(refreshLData);
			refresh.setText("Clear lists");
			refresh.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
			refresh.setBackground(SWTResourceManager.getColor(0, 0, 0));

			Label label5 = new Label(group2, SWT.NONE);
			FormData label5LData = new FormData();
			label5LData.left =  new FormAttachment(0, 1000, 276);
			label5LData.top =  new FormAttachment(0, 1000, 63);
			label5LData.width = 116;
			label5LData.height = 14;
			label5.setLayoutData(label5LData);
			label5.setText("Not included tags");
			label5.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			Label label3 = new Label(group2, SWT.NONE);
			FormData label3LData = new FormData();
			label3LData.left =  new FormAttachment(0, 1000, 179);
			label3LData.top =  new FormAttachment(0, 1000, 63);
			label3LData.width = 85;
			label3LData.height = 14;
			label3.setLayoutData(label3LData);
			label3.setText("Included tags");
			label3.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));

			/* List of included tags properties */
			FormData list3LData = new FormData();
			list3LData.left =  new FormAttachment(0, 1000, 280);
			list3LData.top =  new FormAttachment(0, 1000, 84);
			list3LData.width = 85;
			list3LData.height = 134;
			final List notinclude = new List(group2, SWT.BORDER );
			notinclude.setLayoutData(list3LData);

			/* List of not included tags properties */
			FormData list2LData = new FormData();
			list2LData.left =  new FormAttachment(0, 1000, 184);
			list2LData.top =  new FormAttachment(0, 1000, 84);
			list2LData.width = 81;
			list2LData.height = 134;
			final List include = new List(group2, SWT.BORDER );
			include.setLayoutData(list2LData);

			/* button operator NOT properties */
			final Button not = new Button(group2, SWT.PUSH | SWT.CENTER);
			FormData button3LData1 = new FormData();
			button3LData1.left =  new FormAttachment(0, 1000, 243);
			button3LData1.top =  new FormAttachment(0, 1000, 31);
			button3LData1.width = 37;
			button3LData1.height = 28;
			not.setLayoutData(button3LData1);
			not.setText("NOT");
			not.setFont(SWTResourceManager.getFont("Guttman Adii", 9, 1, false, false));
			not.setBackground(SWTResourceManager.getColor(255, 0, 0));

			/* button operator AND properties */
			final Button and = new Button(group2, SWT.PUSH | SWT.CENTER);
			FormData button2LData = new FormData();
			button2LData.left =  new FormAttachment(0, 1000, 195);
			button2LData.top =  new FormAttachment(0, 1000, 31);
			button2LData.width = 36;
			button2LData.height = 28;
			and.setLayoutData(button2LData);
			and.setText("AND");
			and.setFont(SWTResourceManager.getFont("Guttman Adii", 9, 1, false, false));
			and.setBackground(SWTResourceManager.getColor(128, 128, 192));

			/* Table files properties */
			FormData table1LData = new FormData();
			table1LData.left =  new FormAttachment(0, 1000, 22);
			table1LData.top =  new FormAttachment(0, 1000, 232);
			table1LData.width = 360;
			table1LData.height = 216;
			final Table table1 = new Table(group2, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
			table1.setLinesVisible (true);
			table1.setHeaderVisible (true);
			table1.setLayoutData(table1LData);
			table1.setFont(SWTResourceManager.getFont("Bookman Old Style", 11, 0, false, false));

			TableColumn	tableColumn1 = new TableColumn(table1, SWT.NONE);
			tableColumn1.setText("File");
			tableColumn1.setWidth(195);

			TableColumn	tableColumn2 = new TableColumn(table1, SWT.NONE);
			tableColumn2.setText("Path");
			tableColumn2.setWidth(450);

			/* Combo of tags properties */
			final Combo combo2 = new Combo(group2, SWT.NONE);
			FormLayout combo2Layout = new FormLayout();
			combo2.setLayout(combo2Layout);
			combo2.setText("List of inserted tags");
			FormData combo2LData = new FormData();
			combo2LData.width = 113;
			combo2LData.height = 22;
			combo2LData.left =  new FormAttachment(0, 1000, 14);
			combo2LData.top =  new FormAttachment(0, 1000, 63);
			combo2.setLayoutData(combo2LData);

			final Text text2 = new Text(group2, SWT.NONE);
			FormData text2LData = new FormData();
			text2LData.width = 122;
			text2LData.height = 20;
			text2LData.left =  new FormAttachment(0, 1000, 14);
			text2LData.top =  new FormAttachment(0, 1000, 31);
			text2.setLayoutData(text2LData);

			Label label4 = new Label(group2, SWT.NONE);
			label4.setText("Enter a tag or select one from the list:");
			FormData label4LData = new FormData();
			label4LData.width = 253;
			label4LData.height = 20;
			label4LData.left =  new FormAttachment(0, 1000, 9);
			label4LData.top =  new FormAttachment(0, 1000, 5);
			label4.setLayoutData(label4LData);
			label4.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false, false));

			/* button Enter properties */
			final Button enter = new Button(group2, SWT.PUSH | SWT.CENTER);
			enter.setText("Enter");
			FormData button4LData = new FormData();
			button4LData.left =  new FormAttachment(0, 1000, 305);
			button4LData.top =  new FormAttachment(0, 1000, 21);
			button4LData.width = 62;
			button4LData.height = 38;
			enter.setLayoutData(button4LData);
			enter.setFont(SWTResourceManager.getFont("Guttman Adii", 11, 1, false, false));
			enter.setBackground(SWTResourceManager.getColor(0, 0, 0));

			final Set<String> includedTags  = new HashSet<String>();
			final Set<String> notIncludedTags  = new HashSet<String>();
			final ArrayList<String> allTags =  new ArrayList<String>();

			combo2.addMouseListener(new MouseListener() {
				public void mouseDown(MouseEvent arg0) 
				{
					if (flag==1)
					{

						if (guiCommands.getAllTags() !=null)
						{
							allTags.addAll(guiCommands.getAllTags());
						}
						combo2.removeAll();
						combo2.setText("");
						int len= allTags.size();
						for (int i=0;i<len;i++)
							combo2.add(allTags.get(i));
						allTags.clear();
						flag=0;
					}
				}
				public void mouseDoubleClick(MouseEvent arg0) {	}
				public void mouseUp(MouseEvent arg0) {	}
			});

			/* initialize the window */
			search.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget== search)
					{
						if (guiCommands.getAllTags() !=null)
							allTags.addAll(guiCommands.getAllTags());
						table1.removeAll();
						int len= allTags.size();
						for (int i=0;i<len;i++)
							combo2.add(allTags.get(i));		
					} 
				}
			});	/* of search window listener */

			table1.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) {
					/* get the file from the table, and opening it */
					TableItem item= (TableItem) event.item;
					Desktop desktop = null;
					File file2 = new File(item.getText(1));
					if (Desktop.isDesktopSupported()) {
						desktop = Desktop.getDesktop();
					}
					try
					{
						desktop.open(file2.getParentFile());
					}
					catch (IOException e){
						new ErrorMassage("Error: Can't open this file");
					}
				}
			});

			table1.addMouseMoveListener(new MouseMoveListener() {
				public void mouseMove(MouseEvent arg0) 
				{
					if (table1.getItemCount()>0)
						table1.setToolTipText("To open a file's directory, please just press on it");
				}});

			and.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget==and)
					{
						String tag=text2.getText();
						if ((tag.length()>0)) /* checking that we get a tag */
						{
							if (!includedTags.contains(tag.toString()))
							{
								include.add(tag); /* insert to the list */ 
								includedTags.add(tag); /* insert to the array */
								text2.setText("");
							}
						}
						else
						{
							int index=combo2.getSelectionIndex();
							String tag1;
							if (index>-1)
							{
								tag1=combo2.getItem(index);
								if (!includedTags.contains(tag1.toString()))
								{

									combo2.setText("");
									include.add(tag1); /* insert to the list */
									includedTags.add(tag1); /* insert to the array */
								}
							}
						}
					}
				}
			});	/* of operator AND listener */
			not.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget==not)
					{
						String tag=text2.getText();
						if ((tag.length()>0)) /* checking that we get a tag */
						{
							if (!notIncludedTags.contains(tag.toString()))
							{
								notinclude.add(tag); /* insert to the list */
								notIncludedTags.add(tag); /* insert to the array */
								text2.setText("");
							}
						}
						else
						{
							int index=combo2.getSelectionIndex();
							String tag1;
							if (index>-1)
							{
								tag1=combo2.getItem(index);
								if (!notIncludedTags.contains(tag1.toString()))
								{
									combo2.setText("");
									notinclude.add(tag1); /* insert to the list */
									notIncludedTags.add(tag1); /* insert to the array */
								}
							}
						}
					}
				}
			});	/* of operator NOT listener */

			enter.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget==enter)
					{
						table1.removeAll();
						String tag=text2.getText();
						int index1=combo2.getSelectionIndex();
						if (tag.length()==0 && include.getItemCount()==0 && notinclude.getItemCount()==0 && index1==-1)
							/* sending Error by massageBox */
							new ErrorMassage("Error: Please insert tag first");
						else
						{

							ArrayList<String> files = new ArrayList<String>();
							ArrayList<String> getsFiles;
							if (includedTags == null && notIncludedTags == null)
							{
								new ErrorMassage("No matching file");
								combo2.setText("");
								text2.setText("");
								include.removeAll();
								notinclude.removeAll();
							}
							else
							{
								getsFiles=  guiCommands.searchByTag(includedTags, notIncludedTags);
								if ( getsFiles !=null)
									files.addAll(getsFiles);
								int len= files.size();
								if (len==0)
								{
									new ErrorMassage("No matching file");
									combo2.setText("");
									text2.setText("");
									includedTags.clear();
									notIncludedTags.clear();
									include.removeAll();
									notinclude.removeAll();
								}
								else
								{
									int flag2 =0;
									for (int i=0;i<len;i++)
									{
										String path = files.get(i);
										File f = new File(path);
										if (f.isFile())
										{
											flag2=1;
											TableItem item = new TableItem (table1, SWT.NONE);
											String words[]=path.split("\\\\");
											item.setText (0, words[words.length-1]);
											item.setText (1, path);
										}
									}
									if (flag2==0)
									{
										new ErrorMassage("No matching file");
										include.removeAll();
										notinclude.removeAll();
										combo2.setText("");
										text2.setText("");
									}
									/* clear the lists and the arrays */
									includedTags.clear();
									notIncludedTags.clear();
								}
							}
						}
					}
				}
			});	/* of Enter listener */

			refresh.addListener(SWT.Selection ,new Listener(){
				public void handleEvent(Event event) 
				{
					if(event.widget==refresh)
					{
						/* clear the lists and the arrays */
						combo2.setText("");
						text2.setText("");
						includedTags.clear();
						notIncludedTags.clear();
						include.removeAll();
						notinclude.removeAll();
						table1.removeAll();
					}
				}
			});	/* of refresh listener */
			/**********************************************************************/

			FormData menuLData = new FormData(); 
			menuLData.left =  new FormAttachment(0, 1000, 0); 
			menuLData.top =  new FormAttachment(0, 1000, 0); 
			menuLData.width = 800; 
			menuLData.height = 540; 
			tabfold.setLayoutData(menuLData); 
			tabfold.setSelection(0);

			dialogShell.layout();
			dialogShell.pack();	
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) 
			{
				if (!display.readAndDispatch())
					display.sleep();
			}
			System.exit(0);
		}
		catch (Exception e) {

			e.printStackTrace();
		}

	}

	/**
	 * This function sets it's member guiCommands
	 * @param guiCommands reference of GuiCommands class
	 */
	public void setGuiCommands(GuiCommands guiCommands) {
		this.guiCommands = guiCommands;
	}	

	/**
	 * This function updates the progress bar
	 * @param percent The percent of done tagging work  
	 */
	public static void update(final int percent)
	{
		if (display.isDisposed())
			return;
		display.asyncExec(new Runnable() {
			public void run() {
				if (progressBar1.isDisposed())
					return;
				progressBar1.setSelection(percent);
				PrgrsBar.wake();
			}
		});
	}

} 
